package com.nlp.visualization.service.impl;

import com.nlp.visualization.crawler.pipeline.WeiboSQLPipeline;
import com.nlp.visualization.crawler.processor.ProcessorWeiBoUserInfo;
import com.nlp.visualization.crawler.processor.ProcessorWeiboFlowerAndFollow;
import com.nlp.visualization.pojo.weibo.TWeiboContentMod0;
import com.nlp.visualization.pojo.weibo.TWeiboFollow;
import com.nlp.visualization.pojo.weibo.TWeiboUserinfo;
import com.nlp.visualization.service.IDataWeiboService;
import com.nlp.visualization.service.weibo_mine.IWeiboContentMod0Service;
import com.nlp.visualization.service.weibo_mine.IWeiboFollowService;
import com.nlp.visualization.service.weibo_mine.IWeiboUserinfoService;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LXM
 * @Title: DataWeiboServiceImpl
 * @Description: 微博负责爬虫服务调用的接口实现类
 * @date 2018/3/17下午3:56
 */
@Service
public class DataWeiboServiceImpl implements IDataWeiboService {


    Logger logger = LoggerFactory.getLogger(DataWeiboServiceImpl.class);

    @Autowired
    IWeiboContentMod0Service weiboContentMod0Service;

    @Autowired
    IWeiboFollowService weiboFollowService;

    @Autowired
    IWeiboUserinfoService weiboUserinfoService;

    @Autowired
    WeiboSQLPipeline sqlPipeline;

    @Override
    public void doCrawlWeibo(String uid) {

//        ProcessorWeiBoUserInfo userInfo = new ProcessorWeiBoUserInfo();
//        userInfo.getCookieFromWeiboSite("https://m.weibo.cn/u/" + uid);
        Map map = new HashedMap();
        map.put("Upgrade-Insecure-Requests", 1);
        map.put("Connection", "keep-alive");
        Spider.create(new ProcessorWeiBoUserInfo())
                //从"https://github.com/code4craft"开始抓
//                .addUrl("https://m.weibo.cn/api/container/getIndex?display=0&retcode=6102&type=uid&value=" + uid)
                .addRequest(new Request("https://m.weibo.cn/api/container/getIndex?display=0&retcode=6102&type=uid&value=" + uid).setExtras(map))
                .addPipeline(sqlPipeline)
                //开启20个线程抓取
                .thread(20)
                //启动爬虫
                .run();


    }

    @Override
    public void doCrawFollow(String uid) {

        ProcessorWeiboFlowerAndFollow weiboFlowerAndFollow = new ProcessorWeiboFlowerAndFollow();
        weiboFlowerAndFollow.initProcessor();
        Spider.create(weiboFlowerAndFollow)
                .addUrl("https://m.weibo.cn/api/container/getIndex?display=0&retcode=6102&type=uid&value=" + uid)
                .addPipeline(sqlPipeline)
                //开启20个线程抓取
                .thread(20)
                //启动爬虫
                .run();

    }

    /**
     * 获取所有的微博
     *
     * @param uid
     * @return
     */
    @Override
    public List getAllWeibo(String uid) {
        List<Map> resultList = new ArrayList<>();
        Map filedMap = new HashedMap();
        filedMap.put("user_id", uid);
        List<TWeiboContentMod0> weibiList = weiboContentMod0Service.findAllByField(filedMap);
        for (TWeiboContentMod0 weiboContent : weibiList) {
            Map map = new HashedMap();
            map.put("weibo_item_id", weiboContent.getWeiboItemId());
            map.put("text", weiboContent.getText());
            map.put("publish_time", weiboContent.getPublishTime());
            map.put("devices", weiboContent.getDevices());
            map.put("upvote", weiboContent.getUpvote());
            map.put("repost", weiboContent.getRepost());
            if ("y".equals(weiboContent.getIsResend()))
                map.put("is_resend", "否");
            else
                map.put("is_resend", "是");
            resultList.add(map);

        }
        return resultList;
    }

    /**
     * 获取所有关注者的微博ID和基本信息
     *
     * @param uid
     * @return
     */
    @Override
    public List getAllFollow(String uid, int count) {
        List<Map> resultList = new ArrayList<>();

        //取出所有的关注者的ID
        Map filedMap = new HashedMap();
        filedMap.put("user_id", uid);
        List<TWeiboFollow> userIdList = weiboFollowService.findAllByField(filedMap);
        int i = 0;
        //然后以此取出他们的资料
        for (TWeiboFollow follow : userIdList) {
            try {

                TWeiboUserinfo userinfo = weiboUserinfoService.findById(follow.getTWeiboFollowPK().getFollowId());
                Map map = new HashedMap();
                map.put("user_id", userinfo.getUserId());
                map.put("name", userinfo.getName());
                map.put("image", userinfo.getImageUrl());
                map.put("gender", userinfo.getGender());
                map.put("description", userinfo.getDescription());
                map.put("follow_count", userinfo.getFollowCount());
                map.put("follower_count", userinfo.getFollowerCount());
                map.put("hometown", userinfo.getHometown());
                map.put("job", userinfo.getJob());
                List list_2 = new ArrayList();
                //取它的关注者
                if (i < count) {
                    Map map1 = new HashedMap();
                    map1.put("user_id", follow.getTWeiboFollowPK().getFollowId());
                    List<TWeiboFollow> userIdList_2 = weiboFollowService.findAllByField(map1);
                    if (userIdList_2.size() == 0) {
                        //如果关注者为空的话，那么启用爬虫爬它的关注者
//                    doCrawFollow(userinfo.getUserId());
//                    userIdList_2 = weiboFollowService.findAllByField(map1);
                        continue;
                    }
                    for (TWeiboFollow follow_2 : userIdList_2) {
                        TWeiboUserinfo userinfo_2 = weiboUserinfoService.findById(follow_2.getTWeiboFollowPK().getFollowId());
                        Map map_2 = new HashedMap();
                        map_2.put("user_id", userinfo_2.getUserId());
                        map_2.put("name", userinfo_2.getName());
                        map_2.put("image", userinfo_2.getImageUrl());
                        map_2.put("gender", userinfo_2.getGender());
                        map_2.put("description", userinfo_2.getDescription());
                        map_2.put("follow_count", userinfo_2.getFollowCount());
                        map_2.put("follower_count", userinfo_2.getFollowerCount());
                        map_2.put("hometown", userinfo_2.getHometown());
                        map_2.put("job", userinfo_2.getJob());
                        list_2.add(map_2);
                    }
                }
                map.put("follow", list_2);
                resultList.add(map);
                i++;
            } catch (Exception e) {
                logger.error("获取关注者资料异常", e);
                continue;
            }
        }

        return resultList;
    }

    /**
     * 获取某个微博用户的资料信息
     *
     * @param uid
     * @return
     */
    @Override
    public Map getWeiboUserInfo(String uid) {
        TWeiboUserinfo userinfo = weiboUserinfoService.findById(uid);
        Map map = new HashedMap();
        map.put("user_id", userinfo.getUserId());
        map.put("name", userinfo.getName());
        map.put("image", userinfo.getImageUrl());
        map.put("gender", userinfo.getGender());
        map.put("description", userinfo.getDescription());
        map.put("follow_count", userinfo.getFollowCount());
        map.put("follower_count", userinfo.getFollowerCount());
        map.put("hometown", userinfo.getHometown());
        map.put("job", userinfo.getJob());
        return map;
    }
}
